package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.swagger2.Person;
import cn.wolfcode.wolf2w.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")


@Api(value = "用户资源",description = "用户资源控制器")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;


    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }


    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){

        boolean ret = userInfoService.checkPhone(phone);


        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    @ApiOperation(value = "注册功能",notes = "其实就是新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号",name = "phone",dataType = "String",required = true),
            @ApiImplicitParam(value = "昵称",name = "nickname",dataType = "String",required = true),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true),
            @ApiImplicitParam(value = "确认密码",name = "rpassword",dataType = "String",required = true),
            @ApiImplicitParam(value = "验证",name = "verifyCode",dataType = "String",required = true)
    })
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword, String verifyCode){
        userInfoService.regist(phone,nickname,password,rpassword, verifyCode);
        return JsonResult.success();
    }


    @PostMapping("/login")
    public Object login(String username,String password){
        //user
        UserInfo user = userInfoService.login(username,password);
        //token
        String token = userInfoRedisService.setToken(user);

        Map<String, Object> map = new HashMap<>(); // {}
        map.put("user", user);
        map.put("token", token);

        return JsonResult.success(map);
    }


    @RequireLogin  //代表该方法必须登录才可以访问
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){

        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }


    @RequireLogin
    @GetMapping("/strategies/favor")
    public Object favor(String userId){


        List<String> sidList = strategyStatisVORedisService.getUserStrategyFavor(userId);


        return JsonResult.success(sidList);
    }

    @ApiOperation(value = "SWAGGER测试",notes = "SWAGGER测试")
    @GetMapping("/swagger/test")
    public Object person(Person person){
        return JsonResult.success();
    }
}
